Welcome Guest | Sign in | Register

Home > Java Programming > Operators and Assignments > Questions and Answers

Exercise:

Section 1

01. 1. What will be the output of the program? 

class PassA
{
public static void main(String [] args)
{
PassA p = new PassA();
p.start();
}

void start()
{
long [] a1 = {3,4,5};
long [] a2 = fix(a1);
System.out.print(a1[0] + a1[1] + a1[2] + " ");
System.out.println(a2[0] + a2[1] + a2[2]);
}

long [] fix(long [] a3)
{
a3[1] = 7;
return a3;
}
}
A. 12 15 B. 15 15
C. 345375 D. 375375

Answer and Explanation

Answer: 15 15

Explanation:
The reference variables a1 and a3 refer to the same long array object. When the [1] element is updated in the fix() method, it is updating the array referred to by a1. The reference variable a2 refers to the same array object.
So Output: 3+7+5+" "3+7+5
Output: 15 15 Because Numeric values will be added 

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
02. What will be the output of the program?
class PassS
{
public static void main(String [] args)
{
PassS p = new PassS();
p.start();
}

void start()
{
String s1 = "slip";
String s2 = fix(s1);
System.out.println(s1 + " " + s2);
}

String fix(String s1)
{
s1 = s1 + "stream";
System.out.print(s1 + " ");
return "stream";
}
}
A. slip stream B. slipstream stream
C. stream slip stream D. slipstream slip stream

Answer and Explanation

Answer: slipstream slip stream

Explanation:
When the fix() method is first entered, start()'s s1 and fix()'s s1 reference variables both refer to the same String object (with a value of "slip"). Fix()'s s1 is reassigned to a new object that is created when the concatenation occurs (this second String object has a value of "slipstream"). When the program returns to start(), another String object is created, referred to by s2 and with a value of "stream".

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
03. What will be the output of the program?
class Test
{
public static void main(String [] args)
{
int x= 0;
int y= 0;
for (int z = 0; z < 5; z++)
{
if (( ++x > 2 ) || (++y > 2))
{
x++;
}
}
System.out.println(x + " " + y);
}
}
A. 5 3 B. 8 2
C. 8 3 D. 8 5

Answer and Explanation

Answer: 8 2

Explanation:
The first two iterations of the for loop both x and y are incremented. On the third iteration x is incremented, and for the first time becomes greater than 2. The short circuit or operator || keeps y from ever being incremented again and x is incremented twice on each of the last three iterations.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
04. 4. Which two statements are equivalent?
1. 3/2
2. 3<2
3. 3*4
4. 3<<2
A. 1 and 2 B. 2 and 3
C. 3 and 4 D. 1 and 4

Answer and Explanation

Answer: 3 and 4

Explanation:
(1) is wrong. 3/2 = 1 (integer arithmetic).
(2) is wrong. 3 < 2 = false.
(3) is correct. 3 * 4 = 12.
(4) is correct. 3 <<2= 12. In binary 3 is 11, now shift the bits two places to the left and we get 1100 which is 12 in binary (3*2*2).

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
05. import java.awt.Button;
class CompareReference
{
public static void main(String [] args)
{
float f = 42.0f;
float [] f1 = new float[2];
float [] f2 = new float[2];
float [] f3 = f1;
long x = 42;
f1[0] = 42.0f;
}
}
which three statements are true?

1. f1 == f2
2. f1 == f3
3. f2 == f1[1]
4. x == f1[0]
5. f == f1[0]
A. 1, 2 and 3 B. 2, 4 and 5
C. 3, 4 and 5 D. 1, 4 and 5

Answer and Explanation

Answer: 2, 4 and 5

Explanation:
(2) is correct because the reference variables f1 and f3 refer to the same array object.
(4) is correct because it is legal to compare integer and floating-point types.
(5) is correct because it is legal to compare a variable with an array element.
(3) is incorrect because f2 is an array object and f1[1] is an array element.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
06.  Which two are equal?
1. 32/4
2. (8 >> 2) << 4
3. 2^5
4. 128 >>> 2
5. 2 >> 5
A. 1 and 2 B. 2 and 4
C. 1 and 3 D. 2 and 3

Answer and Explanation

Answer: 2 and 4

Explanation:
(2) and (4) are correct. (2) and (4) both evaluate to 32. (2) is shifting bits right then left using the signed bit shifters >> and <<. (4) is shifting bits using the unsigned operator >>>, but since the beginning number is positive the sign is maintained.
(1) evaluates to 8, (3) looks like 2 to the 5th power, but ^ is the Exclusive OR operator so (3) evaluates to 7. (5) evaluates to 0 (2 >> 5 is not 2 to the 5th).

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum



Partner Sites
LucentBlackBoard.com                  SoftLucent.com                  LucentJobs.com
All rights reserved © 2012-2015 SoftLucent.